using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using iTextSharp.text;

namespace PocoReport
{
    public class Row: IPart, IContainerPart
    {
        public Style Style { get; protected set; }
        public IList<Cell> Cells { get; protected set; }

        public Row()
        {
            Style = new Style();
            Cells = new List<Cell>();
        }

        public void Load(XNode node)
        {
            XElement element = (XElement)node;
            foreach (XElement cellElement in element.Elements("Cell"))
            {
                Cell cell = new Cell();
                cell.Load(cellElement);
                Cells.Add(cell);
            }
        }

        public XNode Save()
        {
            XElement element = new XElement("Row");
            foreach (Cell cell in Cells)
            {
                element.Add(cell.Save());
            }
            return element;
        }

        public IElement GetPdfElement(Document document, Style parentStyle, Scope scope)
        {
            Style mergedStyle = Style.Merge(parentStyle, Style);
            EmptyElement cells = new EmptyElement();
            foreach (Cell cell in Cells)
            {
                cells.Add(cell.GetPdfElement(document, mergedStyle, scope));
            }
            return cells;
        }

        IList<IPart> IContainerPart.Parts
        {
            get
            {
                return new List<IPart>(Cells.Cast<IPart>());
            }
        }
    }
}